//Design By Do Trung --- Email : trungqn19852003@yahoo.com
//Ngày Design :09/12/2011 4:57:03 PM
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using VietStar.Library.Commom.SqlServer;
using Entity = VietStar.Library.Entity;
namespace VietStar.Library.Data
{
	/// <summary>
	/// </summary>
	public class ChiTietHoaDon
{
		public ChiTietHoaDon() {}
				Connection db = new Connection();

/// <summary>
/// Inserts a record into the ChiTietHoaDon table.
/// <summary>
/// <param name="Id"></param>
/// <param name="MaHD"></param>
/// <param name="SanPham"></param>
/// <param name="SoLuong"></param>
/// <param name="ThanhTien"></param>
/// <returns></returns>
	public bool Insert(Entity.ChiTietHoaDon entity)
	{
		bool boolInsert = false;
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_ChiTietHoaDon_Insert";
			SqlCmd.CommandType=CommandType.StoredProcedure;			
			SqlCmd.Parameters.Add("@MaHD", SqlDbType.Decimal).Value=entity.MaHD;
			SqlCmd.Parameters.Add("@SanPham", SqlDbType.Int).Value=entity.SanPham;
			SqlCmd.Parameters.Add("@SoLuong", SqlDbType.Int).Value=entity.SoLuong;
			SqlCmd.Parameters.Add("@ThanhTien", SqlDbType.Float).Value=entity.ThanhTien;

			SqlAdp.InsertCommand = SqlCmd;
			SqlAdp.InsertCommand.ExecuteNonQuery();
			SqlCmd.Transaction.Commit();
			boolInsert = true;
			SqlCnn.Close();
		}
		catch
		{
			boolInsert = false;
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return boolInsert;
	}

/// <summary>
/// Selects a single record from the ChiTietHoaDon table.
/// <summary>
/// <returns>DataTable</returns>
	public DataTable Select()
		{
		DataTable dt = new DataTable();
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_ChiTietHoaDon_Select";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlAdp.SelectCommand = SqlCmd;
			SqlAdp.SelectCommand.ExecuteNonQuery();
			SqlAdp.Fill(dt);
			SqlCmd.Transaction.Commit();
			SqlCnn.Close();
		}
		catch
		{
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return dt;
	}

/// <summary>
/// Selects a single record from the ChiTietHoaDon table.
/// <summary>
/// <returns>DataTable</returns>
	public DataTable SelectPrimaryKey(Entity.ChiTietHoaDon entity)
	{
		DataTable dt = new DataTable();
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_ChiTietHoaDon_SelectPrimaryKey";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlCmd.Parameters.Add("@Id", SqlDbType.Decimal).Value=entity.Id;

			SqlAdp.SelectCommand = SqlCmd;
			SqlAdp.SelectCommand.ExecuteNonQuery();
			SqlAdp.Fill(dt);
			SqlCmd.Transaction.Commit();
			SqlCnn.Close();
		}
		catch
		{
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return dt;
	}

/// <summary>
/// Updates a record in the ChiTietHoaDon table.
/// <summary>
/// <param name="Id"></param>
/// <param name="MaHD"></param>
/// <param name="SanPham"></param>
/// <param name="SoLuong"></param>
/// <param name="ThanhTien"></param>
	public bool Update(Entity.ChiTietHoaDon entity)
	{
		bool boolUpdate = false ;
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_ChiTietHoaDon_Update";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlCmd.Parameters.Add("@Id", SqlDbType.Decimal).Value=entity.Id;
			SqlCmd.Parameters.Add("@MaHD", SqlDbType.Decimal).Value=entity.MaHD;
			SqlCmd.Parameters.Add("@SanPham", SqlDbType.Int).Value=entity.SanPham;
			SqlCmd.Parameters.Add("@SoLuong", SqlDbType.Int).Value=entity.SoLuong;
			SqlCmd.Parameters.Add("@ThanhTien", SqlDbType.Float).Value=entity.ThanhTien;

			SqlAdp.UpdateCommand = SqlCmd;
			SqlAdp.UpdateCommand.ExecuteNonQuery();
			SqlCmd.Transaction.Commit();
			boolUpdate = true ;
			SqlCnn.Close();
		}
		catch
		{
			boolUpdate = false ;
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return boolUpdate;
	}

/// <summary>
		/// Deletes a record from the ChiTietHoaDon table by a composite primary key.
/// <summary>
	public bool Delete(Entity.ChiTietHoaDon entity)
	{
		bool boolDelete = false;
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_ChiTietHoaDon_Delete";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlCmd.Parameters.Add("@Id", SqlDbType.Decimal).Value=entity.Id;

			SqlAdp.DeleteCommand = SqlCmd;
			SqlAdp.DeleteCommand.ExecuteNonQuery();
			SqlCmd.Transaction.Commit();
			boolDelete = true ;
			SqlCnn.Close();
		}
		catch
		{
			boolDelete = false ;
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return boolDelete;
	}
	}
}
